<?php 
   require_once('../mod_config.php');
   /**
    * 
    * Array category
   */
   $array_cate = array('0' => 'Không chọn','1' => '18+');
   
   /**
    * 
    * Class images
   */
   class xinh_images{
      /**
       * 
       * The constructor
      */
      public function __construct(){}
      
      public static function getListImage($limit = 10){
         $db_query = new db_query("SELECT 
                                          xinh_images.id,
                                          xinh_images.link,
                                          xinh_images.view,
                                          xinh_images.active,
                                          xinh_images.hot, 
                                          ilike,
                                          xinh_images.date,
                                          xinh_albums.name,
                                          xinh_albums.id as alb_id,
                                          is_avatar
                                    FROM
                                          xinh_images
                                    INNER JOIN
                                          xinh_albums
                                    ON
                                          xinh_images.alb_id = xinh_albums.id
                                    ORDER BY
                                          id DESC
                                    LIMIT " . $limit);
         $arr_images = $db_query->resultArray();
         unset($db_query);
         return $arr_images;
      }
      
      public static function getListImageByAlbum($alb_id,$limit = 10){
         $db_query = new db_query("SELECT 
                                          xinh_images.id,
                                          xinh_images.link,
                                          xinh_images.view,
                                          xinh_images.active,
                                          xinh_images.hot,
                                          ilike,
                                          xinh_images.date,
                                          xinh_albums.name,
                                          xinh_albums.id as alb_id,
                                          is_avatar
                                    FROM
                                          xinh_images
                                    INNER JOIN
                                          xinh_albums
                                    ON
                                          xinh_images.alb_id = xinh_albums.id
                                    WHERE
                                          xinh_albums.id = ".$alb_id."
                                    ORDER BY
                                          id DESC
                                    LIMIT " . $limit);
         $arr_images = $db_query->resultArray();
         unset($db_query);
         return $arr_images;
      }
      
      public static function getTotalImage(){
         $db_count = new db_count("SELECT 
                                       count(id) as count 
                                    FROM 
                                       xinh_images 
                                    WHERE 
                                       active = 1");
         return $db_count->total;
         unset($db_count);
      }
      
      public static function getTotalImageAlbum($alb_id){
         $db_count = new db_count("SELECT 
                                       count(id) as count 
                                    FROM 
                                       xinh_images 
                                    WHERE 
                                       active = 1
                                    AND
                                       alb_id = " . $alb_id);
         return $db_count->total;
         unset($db_count);
      }
   }
   
   class xinh_albums{
      
      /**
       * 
       * The constructor
      */
      public function __construct(){}
      
      /**
       * 
       * Add albums
       * @param name string
       * @param link_profile string
       * @param cat_id int
       * @param active int
       * @return int row affected
      */
      public static function add($name,$cat_id = 0,$link_profile = '',$active = 1){
         $str_insert = "INSERT INTO xinh_albums(name,
                                                link_profile,
                                                cat_id,
                                                date,
                                                active)
                                    VALUES('" . $name . "',
                                           '" . $link_profile . "',
                                            " . $cat_id . ",
                                            " . time() . ",
                                            0)";  
         $db_insert = new db_execute($str_insert);
         $row_affected = $db_insert->rows_affected;
         unset($db_insert);
         return $row_affected;
      }
      
      
      /**
       * 
       * Search album by LIKE mysql
      */
      public static function searchLike($keyword){
         $arr_albums = array();
         if($keyword != ''){
            $str_query = "SELECT 
                              id,name,date
                           FROM
                              xinh_albums
                           WHERE
                              name LIKE'%" . $keyword . "%'";
            $db_accces = new db_query($str_query);
            while($row = mysql_fetch_assoc($db_accces->result)){
               $arr_albums[] =  $row;
            }
         }
         return $arr_albums;
      }
      
      /**
       * 
       * GET list album
       * @param limit int
       * @return array
      */
      public static function getListAlbum($limit = ''){
         $array_album = array();
         if($limit != '') $limit = 'LIMIT ' . $limit;
         $str_query = "SELECT id,
                              name,
                              date,
                              link_profile,
                              view,
                              alike,
                              total_images,
                              active,
                              cat_id
                           FROM
                              xinh_albums
                           ORDER BY
                              id DESC
                           " . $limit;
         $db_query = new db_query($str_query);
         $array_album = $db_query->resultArray();
         return $array_album;
      }
      
      /**
       * Get total album active
      */
      public static function getTotalAlbums(){
         $db_count = new db_count("SELECT 
                                       count(id) as count 
                                    FROM 
                                       xinh_albums
                                    WHERE 
                                       active = 1");
         return $db_count->total;
         unset($db_count);
      }
      
      /**
       * 
       * Get total image in albums
      */
      public static function getTotalImageInAlbum($alb_id){
         $total = 0;
         if($alb_id > 0){
            $db_access = new db_query("SELECT 
                                          count(id) as count 
                                       FROM 
                                          xinh_images 
                                       WHERE 
                                          active = 1 
                                       AND
                                          alb_id =" . $alb_id );
            if($row = mysql_fetch_assoc($db_access->result) ){
               $total = $row['count'];
            }
         }
         return $total;
      }
      
      /**
       * Get ava album
       * @param id album
      */
      public static function getAvatar($alb_id){
         $image = '';
         if($alb_id > 0){
            $db_access = new db_query("SELECT xinh_images.link FROM xinh_images WHERE alb_id = " . $alb_id);
            if($row = mysql_fetch_assoc($db_access->result)){
               $image = $row['link'];
            }
         }
         return $image;
      }
   }
?>